﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SVNUG.Presentations.NoSql.Business
{
    public static class OrderRepository
    {
        public static void StoreOrder(this Raven.Client.IDocumentSession session, Order order)
        {
            session.Store(order, order.OrderId.ToString());

            //session.SaveChanges();
        }
        public static void RemoveOrder(this Raven.Client.IDocumentSession session, Order order)
        {
            order = session.TryFindOrder(order.OrderId);

            if (order != null)
            {
                session.Delete<Order>(order);
            }

            //session.SaveChanges();
        }
        public static Order TryFindOrder(this Raven.Client.IDocumentSession session, Guid orderId)
        {
            return session.Load<Order>(orderId.ToString());
        }
        public static List<Order> FindAllOrders(this Raven.Client.IDocumentSession session)
        {
            return session.Query<Order>().ToList();
        }

        public static List<Order> TryFindOrdersByCustomerId(this Raven.Client.IDocumentSession session, Guid customerId)
        {
            var result = (from a in session.Query<Order>()
                          where a.CustomerId == customerId
                          select a).ToList();

            return result;
        }
        public static List<Order> FindAllUnshippedOrders(this Raven.Client.IDocumentSession session)
        {
            return (from a in session.Query<Order>()
                    where a.Shipping == null
                    select a).ToList();
        }

        public static void StoreOrder(this RavenDbContext documentStore, Order order)
        {
            using (var session = documentStore.Store.OpenSession())
            {
                session.StoreOrder(order);

                session.SaveChanges();
            }
        }
        public static void RemoveOrder(this RavenDbContext documentStore, Order order)
        {
            using (var session = documentStore.Store.OpenSession())
            {
                session.RemoveOrder(order);

                session.SaveChanges();
            }
        }
        public static Order TryFindOrder(this RavenDbContext documentStore, Guid orderId)
        {
            using (var session = documentStore.Store.OpenSession())
            {
                return session.TryFindOrder(orderId);
            }
        }
        public static List<Order> FindAllOrders(this RavenDbContext documentStore)
        {
            using (var session = documentStore.Store.OpenSession())
            {
                return session.FindAllOrders();
            }
        }
        public static List<Order> TryFindOrdersByCustomerId(this RavenDbContext documentStore, Guid customerId)
        {
            using (var session = documentStore.Store.OpenSession())
            {
                return session.TryFindOrdersByCustomerId(customerId);
            }
        }

        public static List<Order> FindAllUnshippedOrders(this RavenDbContext documentStore)
        {
            using (var session = documentStore.Store.OpenSession())
            {
                return session.FindAllUnshippedOrders();
            }
        }
    }
}
